package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.AlipayConfigOne;

import com.aaa.mrcai.pojo.entity.TestOne;
import com.aaa.mrcai.result.AjaxResult;

import com.aaa.mrcai.service.OrderAddOneService;
import com.aaa.mrcai.service.ZfbTestOneService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;

/**
 * Demo class
 *
 * @Author: Wang
 * @Date: Created in 22:35 2021/12/21
 */
@Service
public class ZfbTestServiceOneImpl implements ZfbTestOneService {

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private OrderAddOneService orderAddOneService;


    private  String result=null;
    private  String resultQuery=null;
    private JSONObject jsonObjectQ= null;
    private Object o1= null;

    @Override
    public AjaxResult zfbtestOne(TestOne testone) {
        try {
            //订单号
            System.out.println(testone);
            String orderId = testone.getSoid();
            System.out.println("wqwwwwwwwww"+orderId);
            //订单总价
//            double total = testone.getOrderpay();
            double total = testone.getTotalPrice();

            System.out.println(total+"9999999");

            String totals = String.valueOf(total);
            String gname = "这是一条订单信息";
            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"+testone);
            //将test存入liushui
            servletContext.setAttribute("liushuiOne",testone);
            System.out.println("hhhhhhh"+testone);
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(
                    AlipayConfigOne.gatewayUrl,
                    AlipayConfigOne.app_id,
                    AlipayConfigOne.merchant_private_key, "json",
                    AlipayConfigOne.charset,
                    AlipayConfigOne.alipay_public_key,
                    AlipayConfigOne.sign_type);

            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(AlipayConfigOne.return_url);
            alipayRequest.setNotifyUrl(AlipayConfigOne.notify_url);

            //商户订单号，商户网站订单系统中唯一订单号，必填
            String out_trade_no = new String(orderId.getBytes("ISO-8859-1"),"UTF-8");
            //付款金额，必填
            String total_amount = new String(totals.getBytes("ISO-8859-1"),"UTF-8");
            //订单名称，必填
            String subject = new String(gname);

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                    + "\"total_amount\":\""+ total_amount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
            //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
            //		+ "\"total_amount\":\""+ total_amount +"\","
            //		+ "\"subject\":\""+ subject +"\","
            //		+ "\"body\":\""+ body +"\","
            //		+ "\"timeout_express\":\"10m\","
            //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

            //请求
            result = alipayClient.pageExecute(alipayRequest).getBody();

            //输出
        }catch (Exception e){
            e.printStackTrace();
        }

        return new AjaxResult(1001,"ok",result);
    }
    @Override
    public AjaxResult queryTestOne() {
        try {

            System.out.println("*****************************");
            TestOne testOne = (TestOne) servletContext.getAttribute("liushuiOne");
            System.out.println("hhhhhhhhhhhhhhhhhhhhhh"+testOne);
            String liushui = testOne.getSoid();
            String liushuiId = String.valueOf(liushui);

            System.out.println(liushui);

            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigOne.gatewayUrl, AlipayConfigOne.app_id, AlipayConfigOne.merchant_private_key, "json", AlipayConfigOne.charset, AlipayConfigOne.alipay_public_key, AlipayConfigOne.sign_type);

            //设置请求参数
            AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

            //商户订单号，商户网站订单系统中唯一订单号
            String out_trade_no = new String(liushuiId);

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"}");

            //请求
            resultQuery = alipayClient.execute(alipayRequest).getBody();


            jsonObjectQ = JSON.parseObject(resultQuery);
            JSONObject o = jsonObjectQ.getJSONObject("alipay_trade_query_response");
            o1 = o.get("code");
            System.out.println(o1);

            if ("10000".equals(o1)){
                return new AjaxResult(10000,"成功下单",null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new AjaxResult(101,"下单失败",null);
    }

    @Override
    public int addOrderOne() {
        TestOne liushui = (TestOne) servletContext.getAttribute("liushuiOne");
        return orderAddOneService.updataOrder(liushui);
    }
}
